#include "SensImg.h"
#include <iostream>
#include <exception>

using namespace Sensor;

static int sens_receiver_count = 0;
SensorReceiver::SensorReceiver(tsens_callback callback) {
  if (sens_receiver_count == 0) {  
    sens_receiver_count = 1;
    port = SENS_PORT;
    sens_open(port, callback);
    start_receiving(port);
  } else {
    throw new std::exception();
  }
}

SensorReceiver::SensorReceiver(tsens_callback callback, int _port) {
  port = _port;
  sens_open(port, callback);
  start_receiving(port);
}

SensorReceiver::~SensorReceiver() {
  stop_receiving(port);
  sens_receiver_count = 0;
}

int SensorReceiver::get_port() {
  return port;
}

SensorPoller::SensorPoller() {
  port = SENS_PORT;
  start_polling(port);
}

SensorPoller::SensorPoller(int _port) {
  port = _port;
  start_polling(port);
}

SensorPoller::~SensorPoller() {
  stop_polling(port);
}

tsens_data SensorPoller::poll() {
  return sens_poll(port);
}


ImgSenderHTTP::ImgSenderHTTP() {
  host = std::string(IMG_HOST);
  port = IMG_PORT;
}

ImgSenderHTTP::ImgSenderHTTP(std::string _host, int _port) {
  host = _host;
  port = _port;
}

int ImgSenderHTTP::send(const char *blob, int bloblen) {
  return img_send_hp(blob, bloblen, host.c_str(), port);
}
